<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Main extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('uwd');
        $this->load->library('parser');
    }

    function index() {
        $data['title'] = 'Universitas Widya Dharma';
        if ($this->session->userdata('login')) {
            $us = $this->session->userdata('username');
            $nama = $this->uwd->nama_mhs($us);
            $data['name'] = 'Loggin as ' . $nama->nama;
        } else {
            $data['name'] = 'E-Learning';
        }
        if ($this->uwd->matkul()) {
            $data['matkul'] = $this->uwd->matkul();
        } else {
            $data['matkul'] = array();
        }
        if ($this->uwd->new_materi()) {
            $data['new'] = $this->uwd->new_materi();
        } else {
            $data['new'] = array();
        }
        if ($this->uwd->top_download()) {
            $data['top'] = $this->uwd->top_download();
        } else {
            $data['top'] = array();
        }
        if ($this->uwd->top_matkul()) {
            $data['top_matkul'] = $this->uwd->top_matkul();
        } else {
            $data['top_matkul'] = array();
        }
        $this->parser->parse('default/head', $data);
        $this->parser->parse('default/main', $data);
        $this->load->view('default/footer');
    }

    function auth() {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password')
        );
        $user = $this->uwd->auth($data);
        $pri = $user->privilegeId;
        if ($pri == 'Dosen') {
            $ses = array(
                'username' => $this->input->post('username'),
                'login' => TRUE,
                'pri' => 2
            );
            $this->session->set_userdata($ses);
            redirect('dosen');
        } elseif ($pri == 'Mahasiswa') {
            $ses = array(
                'username' => $this->input->post('username'),
                'login' => TRUE,
                'pri' => 3
            );
            $this->session->set_userdata($ses);
            redirect('main');
        } else {
            redirect('main');
        }
    }

    function dosen() {
        
    }

    function logout() {
        $this->session->sess_destroy();
        redirect();
    }

    function search() {
        $data['title'] = 'Universitas Widya Dharma';
        $data['name'] = 'E-Learning';
        $d = $this->input->post('key');
        $id = str_replace('%20', ' ', $d);
        if ($this->uwd->cari($id)) {
            $data['matkul'] = $this->uwd->cari($id);
            $data['matakuliah'] = $id;
        } else {
            $data['matkul'] = array();
            $data['matakuliah'] = $id;
        }
        if ($this->uwd->materi_dosen($id)) {
            $data['dosen'] = $this->uwd->materi_dosen($id);
        } else {
            $data['dosen'] = array();
        }
        $this->parser->parse('default/head', $data);
        $this->parser->parse('default/search', $data);
        $this->load->view('default/footer');
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
